﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Utility;

namespace Portal.Exam.Render
{
    public class SearchExamRoomStudent
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {                
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string SelectExamType =
                    "<select id=drpSelectExamType class=\"drplist\" onchange=\"javascript:DetectExamSegment();\">\r\n" +
                    "   <option value=\"exam\">Thi thật</option>\r\n" +
                    "   <option value=\"test\">Thi thử</option>\r\n" +
                    "</select>\r\n";

                StringBuilder RetHtml = new StringBuilder();
                RetHtml.Append(
                #region js
"<script language=javascript> \r\n" +
                    "   function DetectExamSegment()\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       ExamType = document.getElementById(\"drpSelectExamType\").value; \r\n" +
                    "       AjaxOut = Portal.Exam.Render.SearchExamRoomStudent.ServerSideReadingExamSegment(SiteId,ExamType).value; \r\n" +
                    "       document.getElementById('divSegment').innerHTML=AjaxOut.HtmlContent;\r\n" +                    
                    "   }\r\n" +

                    "   function DetectExamRoom()\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       ExamSegment = document.getElementById(\"drpSelectExamSegment\").value; \r\n" +
                    "       AjaxOut = Portal.Exam.Render.SearchExamRoomStudent.ServerSideReadingExamRoom(SiteId,ExamSegment).value; \r\n" +
                    "       document.getElementById('divExamRoom').innerHTML=AjaxOut.HtmlContent;\r\n" +                    
                    "   }\r\n" +
                   

                    "function JumPageExamRoomStudent(PageIndex) { \r\n" +
                    "   document.getElementById('txtPageIndex').value=PageIndex;\r\n" +
                    "   document.getElementById(\"divProcessing\").innerHTML = 'Đang đọc dữ liệu từ máy chủ...'; \r\n" +
                    "   setTimeout(\"JumPageExamRoomStudent()\", 10); \r\n" +
                    "} \r\n" +

                    "function JumPageExamRoomStudent() { \r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    PageIndex = document.getElementById(\"txtPageIndex\").value; \r\n" +
                    "    Keyword = document.getElementById(\"txtKeyword\").value; \r\n" +
                    "    ExamRoom = document.getElementById(\"drpSelectExamRoom\").value; \r\n" +                    
                    "    ExamDate = document.getElementById(\"txtFromDate\").value; \r\n" +
                    "    AjaxOut = Portal.Exam.Render.SearchExamRoomStudent.GetHtml(SiteId, ExamRoom,Keyword,ExamDate, PageIndex).value; \r\n" +                    
                    "    if (AjaxOut.Error) { \r\n" +
                    "        alert(AjaxOut.InfoMessage); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    document.getElementById(\"divProcessing\").innerHTML = \"\"; \r\n" +
                    "    document.getElementById(\"divExamRoomStudentMain\").innerHTML = AjaxOut.HtmlContent; \r\n" +
                    "} \r\n" +
                    

                    "function ClientExamStudentList(ExamRoomId) { \r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    Url = Portal.Exam.Render.ExamRoom.GetUpdateExamStudentUrl(SiteId,ExamRoomId).value; \r\n" +
                    "    if (Url.indexOf(\"ERROR\") != -1) { \r\n" +
                    "        alert(Url); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    window.open(Url, '_self'); \r\n" +
                    "} \r\n" +


                    " function ClientStatus(ExamRoomId,Status)\r\n" +
                    " {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    AjaxOut = Portal.Exam.Render.ExamRoom.ServerSideSetStatus(SiteId,ExamRoomId, Status).value; \r\n" +
                    "    if (AjaxOut.Error) { \r\n" +
                    "        alert(AjaxOut.InfoMessage); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    document.getElementById('spanButton'+ExamRoomId).innerHTML=AjaxOut.HtmlContent;\r\n" +
                    " }\r\n" +

                    " function ClientOpenRoom(ExamRoomId,Status)\r\n" +
                    " {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    AjaxOut = Portal.Exam.Render.ExamRoom.ServerSideSetOpenRoom(SiteId,ExamRoomId, Status).value; \r\n" +
                    "    if (AjaxOut.Error) { \r\n" +
                    "        alert(AjaxOut.InfoMessage); \r\n" +
                    "        return; \r\n" +
                    "    } \r\n" +
                    "    document.getElementById('spanButtonOpenRoom'+ExamRoomId).innerHTML=AjaxOut.HtmlContent;\r\n" +
                    " }\r\n" +

                    "</script>\r\n" +
                #endregion

 " <input id=txtPageIndex type=hidden value=\"0\">\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +                    
                    "            <li onclick=\"javascript:JumPageExamRoomStudent(0);\"><img class=image src=\"" + IconRoot + "/Search.png\"><div>Tìm kiếm</div></li>\r\n" +                    
                    "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=exam','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "  <h3>Tra cứu sinh viên</h3>\r\n" +
                    "  <div class=\"CssFormSaveUpdate\">" +
                    "   <table class=\"tableSearch\"> " +

                     "       <tr>" +
                    "       <td class=\"Col1\">" +
                    "           Hình thức: " +
                    "       </td>" +
                    "       <td>" +
                    "           <span id=divExamType>" + SelectExamType + "</span>" +
                    "       </td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "       <td class=\"Col1\">" +
                    "       Kỳ thi: " +
                    "       </td>" +
                    "       <td>" +
                    "        <span id=divSegment>" + ServerSideReadingExamSegment(SiteId, "exam").HtmlContent + "</span>" +
                    "       </td>" +
                    "       </tr>" +

                    "       <tr>" +
                    "       <td class=\"Col1\">" +
                    "           Môn thi: " +
                    "       </td>" +
                    "       <td>" +
                    "           <span id=divExamRoom>" + ServerSideReadingExamRoom(SiteId,"").HtmlContent + "</span>" +
                    "       </td>" +
                    "       </tr>" +

                    "       <tr>" +
                    "       <td class=\"Col1\">" +
                    "           Mã SV: " +
                    "       </td>" +
                    "       <td>" +
                    "           <input id=txtKeyword class=\"textbox\" > " +
                    "       </td>" +
                    "       </tr>" +                    

                    "       <tr>" +
                    "       <td class=\"Col1\">" +
                    "           Ngày thi: " +
                    "       </td>" +
                    "       <td>" + LookUpUtilities.GenerateLookUpDate("txtFromDate") + "</td>" +
                    "       </tr>" +                  

                    "   </table> " +
                    "</div>\r\n" +
                    "  <div id=divProcessing></div>" +
                    "  <div id=divExamRoomStudentMain class=tabcontent></div>");
                OAjaxOut.HtmlContent = RetHtml.ToString();
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
        
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut GetHtml(
            string SiteId,
            string ExamRoomId,
            string Keyword,
            string ExamDate,
            int PageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (!string.IsNullOrEmpty(ExamDate))
                {
                    if (!FunctionUtilities.checkVnDate(ExamDate))
                    {
                        throw new Exception("Thời gian nhập không hợp lệ!");
                    }
                    //DateTime OExamDate = FunctionUtilities.VNDateToDate(ExamDate);
                }


                DataTable dtExamRoomStudent = new ExamRoomStudentBll().ReadingExamRoomStudentForSearch(SiteId, ExamRoomId, Keyword, ExamDate);
                if (dtExamRoomStudent.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent =
                        "  <div class=CssNoData>Không có phòng thi nào thỏa mãn điều kiện lọc</div>";
                    return OAjaxOut;
                }
                string RoomNo = (string)dtExamRoomStudent.Rows[0]["RoomNo"];                    
                string RetHtml =
                    "   <div class=PageTitle>Số sinh viên trong phòng " + RoomNo + ": " + dtExamRoomStudent.Rows.Count.ToString("#,##0") + "</div>" +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th class=thIndex>#</th>" +
                    "           <th>Mã SV</th>" +
                    "           <th>Tên sinh viên</th>" +
                    "           <th>Giới tính</th>" +
                    "           <th>Ngày sinh</th>" +
                    "           <th>Khóa</th>" +
                    "           <th>Lớp</th>" +
                    "           <th>Ngành</th>" +                    
                    "           <th>Trạng thái</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "       </tr>";

                for (int iIndex = 0; iIndex < dtExamRoomStudent.Rows.Count; iIndex++)
                {
                    string ExamRoomStudentId = (string)dtExamRoomStudent.Rows[iIndex]["ExamRoomStudentId"];
                    string StudentCode = (string)dtExamRoomStudent.Rows[iIndex]["StudentCode"];
                    string StudentName = (string)dtExamRoomStudent.Rows[iIndex]["StudentName"];                    
                    int Sex = (int)dtExamRoomStudent.Rows[iIndex]["Sex"];
                    int FinishExam = (int)dtExamRoomStudent.Rows[iIndex]["FinishExam"];
                    int HasGenQuestion = (int)dtExamRoomStudent.Rows[iIndex]["HasGenQuestion"];
                    DateTime StartExam = new System.DateTime();
                    if (dtExamRoomStudent.Rows[iIndex]["StartExam"] != DBNull.Value)
                    {
                        StartExam = (DateTime)dtExamRoomStudent.Rows[iIndex]["StartExam"];
                    }
                    string Status = "Chưa thi";
                    if (StartExam != new System.DateTime())
                    {
                        if (FinishExam == 0)
                        {
                            Status = "Đang thi";
                        }
                        else
                        {
                            Status = "Thi xong";
                        }
                    }

                    string sDob = "";
                    try
                    {
                        DateTime Dob = (DateTime)dtExamRoomStudent.Rows[iIndex]["Dob"];
                        sDob = Dob.ToString("dd/MM/yyyy");
                    }
                    catch { }
                    string Course = (string)dtExamRoomStudent.Rows[iIndex]["Course"];
                    string TrainingLevel = (string)dtExamRoomStudent.Rows[iIndex]["TrainingLevel"];
                    string Class = (string)dtExamRoomStudent.Rows[iIndex]["Class"];

                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex + 1) + "</td>" +
                    "           <td class=tdCode>" + StudentCode + "</td>" +
                    "           <td>" + StudentName + "</td>" +
                    "           <td>" + (Sex == 1 ? "Nam" : "Nữ") + "</td>" +
                    "           <td class=tdDate>" + sDob + "</td>" +
                    "           <td>" + Course + "</td>" +
                    "           <td>" + Class + "</td>" +
                    "           <td>" + TrainingLevel + "</td>" +
                    //"           <td>" + (HasGenQuestion == 1 ? "Đã tạo" : "Chưa tạo") + "</td>" +
                    "           <td>" + Status + "</td>" +
                    "           <td class=tdButton><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdateExamRoomStudent('" + ExamRoomStudentId + "','" + ExamRoomStudentId + "');\"></td>" +
                    "           <td class=tdButton><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDeleteExamRoomStudent('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                    "           <td class=tdButton><input type=button class=button value=\"Session\" onclick=\"javascript:ClientSetExamRoomStudentSession('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                    "       </tr>";
                }
                RetHtml +=
                    "   </table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = RetHtml;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingExamSegment(string SiteId, string ExamType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dt = new ExamSegmentBll().ReadingExamSegment(SiteId, ExamType, 1, "");
                string SelectExamSegment =
                    "<select id=drpSelectExamSegment class=\"drplist\" onchange=\"javascript:DetectExamRoom();\">\r\n";
                for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
                {
                    string ExamSegmentId = (string)dt.Rows[iIndex]["ExamSegmentId"];
                    string ExamSegmentName = (string)dt.Rows[iIndex]["ExamSegmentName"];
                    SelectExamSegment += "    <option value=\"" + ExamSegmentId + "\">" + ExamSegmentName + "</option>\r\n";
                }
                SelectExamSegment += "</select>\r\n";
                OAjaxOut.HtmlContent = SelectExamSegment;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingExamRoom(string SiteId, string ExamSegmentId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataTable dt = new ExamRoomBll().ReadingExamRoomByExamSegmentId(SiteId, ExamSegmentId);
                string SelectExamRoom =
                    "<select id=drpSelectExamRoom class=\"drplist\">\r\n";
                for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
                {
                    string ExamRoomId = (string)dt.Rows[iIndex]["ExamRoomId"];
                    string ExamRoomTitle = dt.Rows[iIndex]["ExamSubject"] != null ? dt.Rows[iIndex]["ExamSubject"].ToString() : "";
                    SelectExamRoom += "    <option value=\"" + ExamRoomId + "\">" + ExamRoomTitle + "</option>\r\n";
                }
                SelectExamRoom += "</select>\r\n";
                OAjaxOut.HtmlContent = SelectExamRoom;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawGroupQuestion(string SiteId, string RootGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupQuestionCls OGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, RootGroupQuestionId);
                string Html =
                              "<script language=javascript>\r\n" +
                              "     function ClientLoadQuestion(GroupQuestionId)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         document.getElementById('txtSelectedGroupQuestionId').value = GroupQuestionId;\r\n" +
                              "         Keyword=document.getElementById('txtFilterKeyword').value;\r\n" +
                              "         CurrentPageIndex=parseInt(document.getElementById('txtCurrentPageIndex').value);\r\n" +
                              "         AjaxOut = Portal.Exam.Render.GroupQuestionTreeViewRenderBll.ServerSideReadingQuestionInGroupQuestion(SiteId,GroupQuestionId,Keyword, CurrentPageIndex).value;\r\n" +
                              "         document.getElementById('divQuestionList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                              "     }\r\n" +

                              "     function ClientRefresh(CurrentPageIndex)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                              "         document.getElementById('txtCurrentPageIndex').value=CurrentPageIndex;\r\n" +
                              "         ClientLoadQuestion(GroupQuestionId);\r\n" +
                              "     }\r\n" +
                              "</script>\r\n" +
                              "<input type=hidden id=\"txtCurrentPageIndex\" value=\"0\">\r\n" +
                              "<input type=hidden id=\"txtSelectedGroupQuestionId\" value=\"" + RootGroupQuestionId + "\">\r\n" +
                              "<h3><a href=\"GroupQuestionTreeView.aspx?RId=" + RootGroupQuestionId + "\">" + OGroupQuestion.GroupQuestionName + "</a></h3>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\"><a href=\"" + RootGroupQuestionId + "\" target=\"_self\">" + OGroupQuestion.GroupQuestionName + "</a>\r\n" +
                              "     <ul>\r\n";
                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, RootGroupQuestionId);
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" + GroupQuestions[iIndex].GroupQuestionId + "\" target=\"_self\">" + GroupQuestions[iIndex].GroupQuestionName + "</a>\r\n" +
                                BuildSubTreeView(SiteId, GroupQuestions[iIndex].GroupQuestionId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(string SiteId, string ParentGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, ParentGroupQuestionId);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupQuestions[iIndex].GroupQuestionId + "\">" + GroupQuestions[iIndex].GroupQuestionName + "</a>\r\n" +
                                BuildSubTreeView(SiteId, GroupQuestions[iIndex].GroupQuestionId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawFunction(string SiteId, string RootGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string Html =
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddGroupQuestion('" + RootGroupQuestionId + "');\"><img class=image src=\"" + IconRoot + "/Add.png\"><div>Tạo nhóm</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientDeleteGroupQuestion('" + RootGroupQuestionId + "');\"><img class=image src=\"" + IconRoot + "/Delete.png\"><div>Xóa nhóm</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientUpdateGroupQuestion('" + RootGroupQuestionId + "');\"><img class=image src=\"" + IconRoot + "/Update.png\"><div>Sửa nhóm</div></li>\r\n" +

                   "            <li onclick=\"javascript:ClientAddQuestion('" + RootGroupQuestionId + "');\"><img class=image src=\"" + IconRoot + "/Add.png\"><div>Tạo mới câu hỏi</div></li>\r\n" +

                   "            <li onclick=\"javascript:ClientMoveGroupQuestion();\"><img class=image src=\"" + IconRoot + "/Move.png\"><div>Chuyển</div></li>\r\n" +
                    //"            <li onclick=\"javascript:ClientAddQuestionData();\"><img class=image src=\"Resources/Images/AddQuestion.png\"><div>Thêm tài liệu</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientImport();\"><img class=image src=\"" + IconRoot + "/Import.png\"><div>Nhập câu hỏi</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientRefresh(0);\"><img class=image src=\"" + IconRoot + "/Refresh.png\"><div>Đọc lại danh sách</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('GroupQuestion.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingQuestionInGroupQuestion(
            string SiteId,
            string GroupQuestionId,
            string Keyword,
            int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                GroupQuestionCls OGroupQuestion = null;
                if (!string.IsNullOrEmpty(GroupQuestionId))
                {
                    OGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, GroupQuestionId);
                }

                if (OGroupQuestion != null)
                {
                    Html +=
                        "<h3>Đang chọn nhóm: " + OGroupQuestion.GroupQuestionName + "</h3>";
                }
                DataTable dtQuestion = GroupQuestionBll.ReadingQuestionForGroupQuestion(SiteId, GroupQuestionId, Keyword);
                if (dtQuestion.Rows.Count == 0)
                {
                    Html += "<div class=CssNoData>Không có câu hỏi nào thuộc nhóm câu hỏi này</div>\r\n";
                }
                else
                {
                    ReturnPaging OReturnPaging = WebPaging.GetPaging(dtQuestion.Rows.Count, CurrentPageIndex, 25, 10, "ClientRefresh");
                    Html +=
                        OReturnPaging.PagingText +
                           "<div class=tabcontent id=divListQuestionResult>\r\n" +
                           "    <table>\r\n" +
                           "        <tr>\r\n" +
                           "            <th class=thIndex>#</th>\r\n" +
                           "            <th>Câu hỏi</th>\r\n" +
                           "            <th>Kiểu</th>\r\n" +
                           "            <th></th>\r\n" +
                           "            <th></th>\r\n" +
                           "            <th></th>\r\n" +
                        //"            <th></th>\r\n" +
                           "        </tr>\r\n";
                    for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                    {
                        string QuestionId = (string)dtQuestion.Rows[iIndex]["QuestionId"];
                        string Type = (string)dtQuestion.Rows[iIndex]["Type"];
                        string Title = (string)dtQuestion.Rows[iIndex]["Title"];
                        Html +=
                           "        <tr>\r\n" +
                           "            <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                           "            <td><span class=CssQuestionSubject><a href=\"#\">" + Title + "</a></span></td>\r\n" +
                           "            <td>" + Type + "</td>\r\n" +
                           "            <td class=tdButton><a href=\"javascript:DeleteQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Xóa câu hỏi\" title=\"Xóa câu hỏi\" class=imagebutton src=\"" + IconRoot + "/delete.png\"></a></td>\r\n" +
                           "            <td class=tdButton><a href=\"javascript:UpdateQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Sửa câu hỏi\" title=\"Sửa\" class=imagebutton src=\"" + IconRoot + "/Update.png\"></a></td>\r\n" +
                           "            <td class=tdButton><a href=\"javascript:ViewQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Xem câu hỏi\" title=\"Xem câu hỏi\" class=imagebutton src=\"" + IconRoot + "/preview.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"AddDataToGroupQuestion.aspx?GroupQuestionId=" + GroupQuestionId + "&QuestionId=" + QuestionId + "\"><img alt=\"Phân vào nhóm\" title=\"Phân vào nhóm\" class=imagebutton src=\"Resources/Images/Category.png\"></a></td>\r\n" +
                           "        </tr>\r\n";
                    }
                    Html += "</table>\r\n" +
                            "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }





        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerSideGetAddGroupQuestion(
        //    string SiteId,
        //    string RootGroupQuestionId,
        //    string ParentGroupQuestionId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        OAjaxOut =
        //            GroupQuestionRenderBll.DrawAddGroupQuestionForAjax(SiteId, RootGroupQuestionId, ParentGroupQuestionId, "javascript:BackFromAddGroupQuestion();");
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetAddQuestion(
            string SiteId,
            string RootGroupQuestionId,
            string ParentGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut.RetUrl = "AddQuestion.aspx?RootGroupQuestionId=" + RootGroupQuestionId + "&GroupQuestionId=" + ParentGroupQuestionId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetImportGroupQuestion(
            string SiteId,
            string RootGroupQuestionId,
            string ParentGroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupQuestionCls ORootGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, RootGroupQuestionId);
                GroupQuestionCls OSubGroupQuestion = GroupQuestionBll.CreateGroupQuestionModel(SiteId, ParentGroupQuestionId);
                string DisplayText = ORootGroupQuestion.GroupQuestionName;
                if (OSubGroupQuestion != null)
                {
                    if (!OSubGroupQuestion.GroupQuestionId.Equals(ORootGroupQuestion.GroupQuestionId))
                    {
                        DisplayText += " / " + OSubGroupQuestion.GroupQuestionName;
                    }
                }

                string[] Types = new string[]{
                    "BestAnswer","YesNo"//,"MultiChoice"
                };
                string[] SubjectTypes = new string[]{
                    "Câu hỏi 1 phương án đúng nhất","Câu hỏi dạng đúng hoặc sai"//,"Các đáp án đúng"
                };

                string SelectTypeText = "<select class=drplist id=drpSelectType onchange=javascript:JumPageGroupQuestionTreeViewRenderBll(0);>";
                for (int iIndex = 0; iIndex < Types.Length; iIndex++)
                {
                    SelectTypeText += "<option value=\"" + Types[iIndex] + "\">" + SubjectTypes[iIndex] + " (" + Types[iIndex] + ")</option>";
                }
                SelectTypeText += "</select>";


                string SelectLevelText =
                    "<select id=drpSelectLevelGroupQuestionTreeViewRenderBll class=drplist>\r\n" +
                    "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                    "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                    "   <option value=\"2\">Mức độ khó</option>\r\n" +
                    "</select>\r\n";


                string SelectOverwriteText =
                    "<select id=drpSelectOverwrite class=drplist>\r\n" +
                    "   <option value=\"0\">Không đè nếu tồn tại</option>\r\n" +
                    "   <option value=\"1\">Ghi đè nếu tồn tại</option>\r\n" +
                    "</select>\r\n";

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string RetHtml =
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientExecuteGroupQuestionTreeViewRenderBll();\"><img class=image src=\"" + IconRoot + "/Execute.png\"><div>Nhập câu hỏi</div></li>\r\n" +
                    "            <li onclick=\"javascript:ClientBackFromImport();\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    " </div>\r\n" +
                    " <div class=CssFormSaveUpdate>\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <td class=Col1>Thuộc thư mục:</td>" +
                    "           <td class=Col2>" + DisplayText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Kiểu câu hỏi:</td>" +
                    "           <td class=Col2>" + SelectTypeText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Mức độ:</td>" +
                    "           <td class=Col2>" + SelectLevelText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Tham số:</td>" +
                    "           <td class=Col2>" + SelectOverwriteText + "</td>" +
                    "       </tr>" +
                    "       <tr>\r\n" +
                    "           <td class=Col1>Chọn tệp:</td>\r\n" +
                    "           <td class=Col2>\r\n" +
                    "                   <input type=\"file\" name=\"fileToUpload[]\" id=\"fileToUpload\" onchange=\"fileSelected();\" /><div style=\"margin-top:10px;\"></div>\r\n" +//<input type=button class=button onclick=\"uploadFile()\" value=\"Thực hiện nhập dữ liệu từ tệp\" /><input type=button class=button onclick=\"SaveLog()\" value=\"Ghi Log import\" />
                    "                   <div id=divLinkSaveLog></div>\r\n" +
                    "                   <div id=\"fileName\"></div>\r\n" +
                    "                   <div id=\"fileSize\"></div>\r\n" +
                    "                   <div id=\"fileType\"></div>\r\n" +
                    "                   <div id=divProcessing></div>\r\n" +
                    "                   <div id=\"divImportLog\" style=\"padding:4px;border:solid 1px silver;height:200px;overflow:auto;display:none;\"></div>\r\n" +
                    "                   <input type=hidden id=txtImportFile value=\"\">\r\n" +
                    "                   <div class=CssTip style=\"color:maroon;font-size:12px;\"><i>(*) Chú ý: File phải đúng định dạng quy định theo <a href=\"ExcelTemplate/ExamStudentList.xls\">biểu mẫu</a> sau đó save as thành file text với định dạng unicode và dùng file này để import</i></div>\r\n" +
                    "           </td>\r\n" +
                    "       </tr>\r\n" +
                    "   </table>" +
                    " </div>\r\n" +
                    " <div id=divProcessingImport></div>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetUpdateGroupQuestion(
            string SiteId,
            string GroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut =
                    GroupQuestionRenderBll.DrawUpdateGroupQuestionForAjax(SiteId, GroupQuestionId, "javascript:BackFromUpdateGroupQuestion();");
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteGroupQuestion(
            string SiteId,
            string GroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupQuestionBll.DeleteGroupQuestion(SiteId, GroupQuestionId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideGetSearchForm(
            string SiteId,
            string RootGroupQuestionId,
            string GroupQuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                OAjaxOut.HtmlContent = SearchExamQuestion.Draw(SiteId, GroupQuestionId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteQuestion(
            string SiteId,
            string GroupQuestionId,
            string QuestionId,
            string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                new QuestionBll().DeleteQuestion(null, SiteId, QuestionId);
                OAjaxOut.HtmlContent = ServerSideReadingQuestionInGroupQuestion(SiteId, GroupQuestionId, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideUpdateQuestion(
            string SiteId,
            string GroupQuestionId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string UpdateUrl = "UpdateQuestion.aspx?QuestionId=" + QuestionId + "&RootGroupQuestionId=" + GroupQuestionId;
                OAjaxOut.RetUrl = UpdateUrl;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideViewQuestion(
            string SiteId,
            string GroupQuestionId,
            string QuestionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string UpdateUrl = "PreviewOneQuestion.aspx?QuestionId=" + QuestionId + "&RootGroupQuestionId=" + GroupQuestionId;
                OAjaxOut.RetUrl = UpdateUrl;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        //import file
        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadMaxLine(string SiteId, string ImportFile)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(ImportFile);
                OAjaxOut.RetNumber = ds.Tables["Question"].Rows.Count;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static string ServerSideGetProcessBar(string SiteId, int ProcessLine, int MaxLine)
        {
            int MaxW = 700;
            decimal W = 0;
            decimal Percent = 0;
            if (MaxLine > 0)
            {
                W = (decimal)MaxW * (decimal)ProcessLine / (decimal)MaxLine;
                Percent = ((decimal)ProcessLine / (decimal)MaxLine) * 100;
            }
            if (Percent < 1) Percent = 1;
            if (Percent > 100) Percent = 100;
            string HtmlProgressBar = "<div style=\"width:" + MaxW + "px;padding:10px; background-color:silver;border:solid 1px black;height:55px;margin-top:10px;margin-bottom:10px;\">\r\n" +
                                     "   <div style=\"padding:4px;\">Xử lý dòng " + (ProcessLine + 1) + "/" + MaxLine + "</div>\r\n" +
                                     "   <div style='border:solid 1px black;padding:4px;background-color:blue;color:white;width:" + W.ToString("###0") + "px'>" + Percent.ToString("#,##0") + "% </div>\r\n" +
                                     "</div>\r\n";

            return HtmlProgressBar;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideProcessLine(
            string SiteId,
            string GroupQuestionId,
            string QuestionType,
            string LevelImport,
            string ImportFile,
            int ProcessLine,
            int Overwrite)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            OAjaxOut.InfoMessage = "";
            QuestionCls OQuestion = new QuestionCls();
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(ImportFile);
                DataTable dtQuestion = ds.Tables["Question"];
                if (dtQuestion == null)
                {
                    return OAjaxOut;
                }
                if (dtQuestion.Rows.Count == 0)
                {
                    return OAjaxOut;
                }
                if (ProcessLine == dtQuestion.Rows.Count)
                {
                    return OAjaxOut;
                }

                DataTable dtAnswer = ds.Tables["Answer"];
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                string QuestionId = (string)dtQuestion.Rows[ProcessLine]["Id"];
                DataView dvAnswer = new DataView(dtAnswer, "QuestionId='" + QuestionId + "'", "", DataViewRowState.CurrentRows);

                OQuestion.QuestionId = QuestionId;
                OQuestion.Title = (string)dtQuestion.Rows[ProcessLine]["Subject"];
                OQuestion.Type = QuestionType;
                OQuestion.Level = int.Parse(LevelImport);
                OQuestion.Answers = new System.Collections.ObjectModel.Collection<AnswerCls> { };
                for (int iIndexAnswer = 0; iIndexAnswer < dvAnswer.Count; iIndexAnswer++)
                {
                    AnswerCls OAnswer = new AnswerCls();
                    OAnswer.AnswerId = (string)dvAnswer[iIndexAnswer]["AnswerId"];
                    OAnswer.AnswerTextFirst = (string)dvAnswer[iIndexAnswer]["AnswerText"];
                    bool Correct = bool.Parse(dvAnswer[iIndexAnswer]["IsAnswer"].ToString());
                    if (Correct)
                    {
                        OQuestion.CorrectOrder = iIndexAnswer;// ?1:0;
                    }
                    OAnswer.CorrectOrder = Correct ? 1 : 0;
                    OAnswer.IndexOrder = iIndexAnswer + 1;
                    if (string.IsNullOrEmpty(OAnswer.AnswerTextFirst) && string.IsNullOrEmpty(OAnswer.AnswerTextLast))
                    {
                        //trang khong lam gi ca
                    }
                    else
                    {
                        OQuestion.Answers.Add(OAnswer);
                    }
                }
                new QuestionBll().AddQuestion(null, SiteId, GroupQuestionId, QuestionType, int.Parse(LevelImport), OQuestion, Overwrite);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                string ErrorMsg = ex.Message.ToString();
                if (ErrorMsg.IndexOf("Violation of PRIMARY KEY") != -1)
                {
                    ErrorMsg = "Đã có câu hỏi '" + OQuestion.Title + "' trong cơ sở dữ liệu";
                }
                OAjaxOut.InfoMessage = "Lỗi xử lý dòng " + (ProcessLine + 1) + " => " + ErrorMsg;
            }
            return OAjaxOut;
        }
    }
}
